% 定义适应度函数（fitness function）
function fitness = fitnessFunction(x)
    % x: 粒子位置向量 [t_1x, t_1y, ..., t_Nx, t_Ny, r_x, r_y]
    % 这里假设传输天线的位置是 t = [x(1), x(2), ..., x(2*N-1), x(2*N)]
    % 接收天线位置是 r = [x(2*N+1), x(2*N+2)]
    
    % 随机生成信道向量 h 和 h_w 作为示例
    h = randn(N, 1) + 1j * randn(N, 1);
    h_w = randn(N, 1) + 1j * randn(N, 1);
    
    % 求解最优波束形成向量 w
    cvx_begin quiet
        variable W(N, N) hermitian semidefinite
        maximize(trace(h * h' * W))
        subject to
            trace(W) <= p_max;
            trace(h_w * h_w' * W) <= p_th;
    cvx_end
    
    % 检查 cvx 求解结果
    if strcmp(cvx_status, 'Solved')
        % 计算适应度值
        fitness = -cvx_optval; % 由于粒子群算法是最小化问题，所以取负值
    else
        fitness = Inf; % 如果求解失败，设置为无穷大
    end
    
    % 添加惩罚函数以满足最小间距约束
    penalty = 0;
    for i = 1:N
        for j = i+1:N
            dist = norm([x(2*i-1) - x(2*j-1), x(2*i) - x(2*j)]);
            if dist < Gamma * d
                penalty = penalty + 1e6; % 惩罚项
            end
        end
    end
    fitness = fitness + penalty;
end